<template>
  <van-button
    v-if="isFollowed"
    class="follow-btn"
    round
    size="small"
    @click="onFollow"
    :loading="followLoading"
    >已关注</van-button
  >
  <van-button
    @click="onFollow"
    v-else
    class="follow-btn"
    :loading="followLoading"
    type="info"
    color="#3296fa"
    round
    size="small"
    icon="plus"
    >关注</van-button
  >
</template>

<script>
import { addFollw, deleteFollow } from "@/api/user";

export default {
  name: "FollowUser",
  model: {
    prop: "isFollowed",
    event: "update-is_followed",
  },
  props: {
    isFollowed: {
      type: Boolean,
      required: true,
    },
    userId: {
      type: [String, Number, Object],
      required: true,
    },
  },
  data() {
    return {
      followLoading: false,
    };
  },
  methods: {
    // 关注
    async onFollow() {
      this.followLoading = true;
      try {
        if (this.isFollowed) {
          // 已关注 取消关注
          await deleteFollow(this.userId);
          // this.article.is_followed = false
        } else {
          // 未关注 点击关注
          await addFollw(this.userId);
          // this.article.is_followed = true
        }
        // this.isFollowed = !this.isFollowed;
        this.$emit("update-is_followed", !this.isFollowed);
      } catch (err) {
        let message = "操作失败，请重试";
        if (err.response && err.response.status === 400) {
          message = "你不能关注你自己!";
        }
        this.$toast(message);
      }
      this.followLoading = false;
    },
  },
};
</script>

<style></style>
